﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Data.OleDb;
using System.Data;

namespace PayRoller.Manage.OrgInfo
{
    public class ImpPay : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Button bnUpload;
        protected FileUpload FileUpload1;
        protected HtmlForm form1;
        protected Label lblTip;
        protected TextBox txtPath;

        // Methods
        protected void bnUpload_Click(object sender, EventArgs e)
        {
            if ((this.FileUpload1.PostedFile.FileName != null) && (this.FileUpload1.PostedFile.FileName != ""))
            {
                string str = "";
                int num = this.FileUpload1.PostedFile.FileName.LastIndexOf(".");
                str = this.FileUpload1.PostedFile.FileName.Substring(num + 1);
                string str2 = "";
                if (str == "xls")
                {
                    str2 = Guid.NewGuid().ToString() + "." + str;
                    this.FileUpload1.PostedFile.SaveAs(base.Server.MapPath("~/" + ConfigurationManager.AppSettings["upPath"] + "/" + str2));
                    this.txtPath.Text = ConfigurationManager.AppSettings["upPath2"] + "/" + str2;
                    this.ImportEmpBank(this.ExcelToDS(this.txtPath.Text), Convert.ToInt32(base.Request["OrgSid"]));
                    base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Success!');</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('请选择Excel文件!');</script>");
                }
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('请选择Excel文件!');</script>");
            }
        }

        public DataSet ExcelToDS(string Path)
        {
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
            new OleDbConnection(connectionString).Open();
            string selectCommandText = "";
            OleDbDataAdapter adapter = null;
            DataSet dataSet = null;
            selectCommandText = "select * from [sheet1$]";
            adapter = new OleDbDataAdapter(selectCommandText, connectionString);
            dataSet = new DataSet();
            adapter.Fill(dataSet, "table1");
            return dataSet;
        }

        public void ImportEmpBank(DataSet ds, int OrgSid)
        {
            DataTable table = ds.Tables[0];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                try
                {
                    string str = table.Rows[i][0].ToString();
                    string str2 = table.Rows[i][1].ToString();
                    string str3 = table.Rows[i][2].ToString();
                    string str4 = table.Rows[i][3].ToString();
                    string str5 = table.Rows[i][4].ToString();
                    string str6 = table.Rows[i][5].ToString();
                    decimal num2 = Convert.ToDecimal(table.Rows[i][6]);
                    string str7 = table.Rows[i][7].ToString();
                    TempinfoBN obn = new TempinfoBN(this.Page);
                    TempinfoDT detail = new TempinfoDT();
                    detail.OrgSid = OrgSid;
                    detail.OrgEmpSid = str2;
                    detail.EmpName = str;
                    int num3 = obn.AddId(detail);
                    obn.Close();
                    obn.Dispose();
                    TbankinfoBN obn2 = new TbankinfoBN(this.Page);
                    TbankinfoDT odt2 = new TbankinfoDT();
                    odt2.EmpSid = num3;
                    odt2.AccountName = str4;
                    odt2.AccountNum = str6;
                    odt2.Bsb = str5;
                    odt2.PayReference = str7;
                    int num4 = obn2.AddId(odt2);
                    obn2.Close();
                    obn2.Dispose();
                    TemppaymentBN tbn = new TemppaymentBN(this.Page);
                    TemppaymentDT tdt = new TemppaymentDT();
                    tdt.EmpSid = num3;
                    tdt.BankSid = num4;
                    tdt.PaymentDate = Convert.ToDateTime( str3);
                    tdt.PaymentAmount = num2;
                    tbn.Add(tdt);
                    tbn.Close();
                    tbn.Dispose();
                }
                catch (Exception exception)
                {
                    this.lblTip.Text = exception.Message;
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "0010";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }
}

